package com.movitech.mobile.cz.base.modules.activiti.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.movitech.mobile.cz.base.modules.activiti.entity.AProcessNodeLineConfig;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.movitech.mobile.cz.base.common.utils.ActivitiConstant;
import com.movitech.mobile.cz.base.common.utils.StringUtils;
import com.movitech.mobile.cz.base.modules.activiti.dao.AProcessNodeConfigMapper;
import com.movitech.mobile.cz.base.modules.activiti.entity.AProcessNodeConfig;
import com.movitech.mobile.cz.base.modules.activiti.entity.AProcessNodeConfigExample;

@Service
public class AProcessNodeConfigService {
    protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private AProcessNodeConfigMapper aProcessNodeConfigMapper;
	@Autowired
	private TaskService taskService;
	
    /***
     * 保存AProcessNodeConfig对象
     * @param aProcessNodeConfig
     */
    public void saveAProcessNodeConfig(AProcessNodeConfig aProcessNodeConfig) {
    	aProcessNodeConfigMapper.insertSelective(aProcessNodeConfig);
    }

	public AProcessNodeConfig selectById(String id) {
		AProcessNodeConfig aProcessNodeConfig = null;
		if(StringUtils.isNotEmpty(id)){
			aProcessNodeConfig = aProcessNodeConfigMapper.selectByPrimaryKey(id);
		}
		return aProcessNodeConfig;
	}

    /***
     * 更新AProcessNodeConfig对象
     * @param aProcessNodeConfig
     */
    public void updateAProcessNodeConfig(AProcessNodeConfig aProcessNodeConfig) {
    	aProcessNodeConfigMapper.updateByPrimaryKeySelective(aProcessNodeConfig);
    }
    
    
	/***
	 * 根据Activiti流程配置对象id，获取Activiti流程节点配置列表
	 * @param processConfigId
	 * @return
	 */
	public List<AProcessNodeConfig> getAProcessNodeConfigByProcessConfigId(String processConfigId) {
		AProcessNodeConfigExample filter = new AProcessNodeConfigExample();
		filter.createCriteria().andProcessConfigIdEqualTo(processConfigId);
		return aProcessNodeConfigMapper.selectByExample(filter);
	}
	
	/***
	 * 根据Activiti流程配置对象id/节点key，获取Activiti流程节点配置列表
	 * @param processConfigId
	 * @param nodeKey
	 * @return
	 */
	public AProcessNodeConfig getAProcessNodeConfigByNodeKey(String processConfigId,String nodeKey) {
		AProcessNodeConfig aProcessNodeConfig = null;
		if(StringUtils.isNotEmpty(processConfigId) && StringUtils.isNotEmpty(nodeKey)) {
			AProcessNodeConfigExample filter = new AProcessNodeConfigExample();
			filter.createCriteria().andProcessConfigIdEqualTo(processConfigId).andNodeKeyEqualTo(nodeKey);
			List<AProcessNodeConfig> lstAProcessNodeConfig = aProcessNodeConfigMapper.selectByExample(filter);
			if(lstAProcessNodeConfig!=null && lstAProcessNodeConfig.size()>0) {
				aProcessNodeConfig = lstAProcessNodeConfig.get(0);
			}
		}
		return aProcessNodeConfig;
	}

	/***
	 * 构建节点key与人员的map
	 * @param processConfigId
	 * @return
	 */
    public Map<String,List<String>> getNodeKeyUserMap(String processConfigId) {
    	Map<String,List<String>> mapResult = new HashMap<String,List<String>>();
    	List<AProcessNodeConfig> lstAProcessNodeConfig = getAProcessNodeConfigByProcessConfigId(processConfigId);
    	if(lstAProcessNodeConfig!=null && lstAProcessNodeConfig.size()>0) {
    		for (AProcessNodeConfig aProcessNodeConfig : lstAProcessNodeConfig) {
    			if (NumberUtils.toInt(aProcessNodeConfig.getNodeCountsignFlag() + "") == ActivitiConstant.YES_INTEGER) {
    				if(NumberUtils.toInt(aProcessNodeConfig.getNodeUserGroupsType()+"") == ActivitiConstant.NODE_USER_TYPE_USERS) {
    					mapResult.put(aProcessNodeConfig.getNodeKey(), StringUtils.getListBySplitStr(aProcessNodeConfig.getNodeUserGroups()));
    				}else {
    					mapResult.put(aProcessNodeConfig.getNodeKey(), new ArrayList<String>());
    				}
				}
			}
    	}
    	return mapResult;
    }
    
    /***
     * 当前节点是否会签
     * @param processConfigId
     * @param taskId
     * @return
     */
    public boolean isCountSign(String processConfigId,String taskId) {
    	boolean isCountSign = false;
    	if(StringUtils.isNotEmpty(processConfigId) && StringUtils.isNotEmpty(taskId)) {
	    	Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
			if(task!=null) {
				AProcessNodeConfig aProcessNodeConfig = getAProcessNodeConfigByNodeKey(processConfigId, task.getTaskDefinitionKey());
				if(aProcessNodeConfig!=null) {
					if (NumberUtils.toInt(aProcessNodeConfig.getNodeCountsignFlag() + "") == ActivitiConstant.YES_INTEGER) {
	    				if(NumberUtils.toInt(aProcessNodeConfig.getNodeUserGroupsType()+"") == ActivitiConstant.NODE_USER_TYPE_USERS) {
	    					isCountSign = true;
	    				}
					}
				}
			}
    	}
		return isCountSign;
    }
}